<?php
include("../conn.php");
include 'a-cookie.php';

$method = $_POST['method'];
$order8 = null;
$order9 = null;
$order10 = null;
$order11 = null;
$remak = null;
$pro_id = null;
echo $aid = $_SESSION['a-id'];
//先把表中的数据清空
//$sql_all_delete=mysql_query("delete from dzsubject");


//根据答辩评分的表查找所以元素并求平均
if ($method == 'all') {
    $sql_avg = "SELECT ReplyMark.Project_ProjectId,ReplyMark.remark,
		avg(Order8) avg_order8,avg(Order9) avg_order9,avg(Order10) avg_order10,avg(Order11) avg_order11, count(*) count,
		Project.ProjectName, teacher.TeacherName, student.StudentName, student.StudentId
		FROM ReplyMark
		LEFT JOIN Project ON ReplyMark.Project_ProjectId = project.ProjectId
		LEFT JOIN student ON project.Student_StudentId = student.StudentId
		LEFT JOIN teacher ON student.Teacher_TeacherId = teacher.TeacherId
		group by Project_ProjectId order by Project_ProjectId";
    $result_avg = mysqli_query($conn, $sql_avg);
    while ($row_avg = mysqli_fetch_array($result_avg)) {
        $pro_id = $row_avg['Project_ProjectId'];
        $proname = $row_avg['ProjectName'];
        $order8 = round($row_avg['avg_order8'], 2);
        $order9 = round($row_avg['avg_order9'], 2);
        $order10 = round($row_avg['avg_order10'], 2);
        $order11 = round($row_avg['avg_order11'], 2);
        $remak = $row_avg['remark'];


        $sql_all_insert = "update  dzsubject set PorjectName='$proname',order8='$order8',Order9='$order9',Order10='$order10',Order11='$order11',remark='$remak'  where DZPorjectId=  $pro_id";

        if ($conn->query($sql_all_insert)) {

        } else {
            echo "论文修改失败";
        }

    }
} else if ($method == 'one') {
    //部分答辩汇总
    $sql_avg = "    SELECT s.StudentId, Project_ProjectId, AVG( r.Order8 ) AS order8, AVG( r.Order9 ) AS order9, AVG( r.Order10 ) AS order10, AVG( r.Order11 ) AS order11, r.remark,t.TeacherId,p.ProjectName,p.ProjectId
FROM replymark AS r
  JOIN project AS p ON r.Project_ProjectId = p.ProjectId
  JOIN student AS s ON p.Student_StudentId = s.StudentId
  JOIN teacher as t on t.TeacherId = r.Teacher_TeacherId
GROUP BY r.Project_ProjectId
HAVING  t.TeacherId=$aid";
}
$result_avg = $conn->query($sql_avg);
while ($row_avg = $result_avg->fetch_array()) {
    $pid = $row_avg['ProjectId'];
    $proname = $row_avg['ProjectName'];
    $order8 = $row_avg[2];
    $order9 = $row_avg[3];
    $order10 = $row_avg[4];
    $order11 = $row_avg[5];
    $remak = $row_avg[6];

    $csql = "UPDATE dzsubject set PorjectName='$proname',Order8='$order8',Order9='$order9',Order10='$order10',Order11='$order11',remark='$remak' WHERE DZPorjectId='$pid'";
    if ($conn->query($csql)) {
        echo '论文修改成功';
    }
}
echo "论文修改成功";

//


?>
